package com.fjwt.gz.proxy.ctrl.anon;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.DBOEMConfig;
import com.fjwt.gz.core.service.ISysConfigService;
import com.fjwt.gz.core.utils.JsonKit;
import com.fjwt.gz.db.entity.SysConfig;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.SysConfigService;
import com.fjwt.gz.service.mapper.SysConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
* 网站信息ctrl
*
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2022/2/14 20:56
*/
@RestController
@RequestMapping("/api/anon/siteInfos")
@Slf4j
public class SiteInfoController extends CommonCtrl {

	@Autowired private ISysConfigService sysConfigService;

	@Autowired
	private SysConfigMapper sysConfigMapper;

	@GetMapping()
	public ApiRes getSiteInfos() throws BizException {

		String scheme = request.getScheme();

		String serverName = request.getServerName();
		log.info("机构端获取网站信息，请求地址：/api/anon/siteInfos，请求参数：scheme：{}，serverName：{}", scheme, serverName);
		DBOEMConfig dboemConfig = sysConfigService.getOemConfig();

		LambdaQueryWrapper<SysConfig> queryWrapper =new LambdaQueryWrapper<>();
		queryWrapper.eq(SysConfig::getConfigKey,"httpMessageEncryptFlag");
		SysConfig config = sysConfigMapper.selectOne(queryWrapper);
		JSONObject result = new JSONObject();
		result.put("siteInfo", dboemConfig);
		result.put("sysConfig", JsonKit.newJson("httpMsgIsEncrypt", SysConfigService.HTTP_MSG_IS_ENCRYPT));
		if(Objects.isNull(config)){
			result.put("sysConfig", JsonKit.newJson("httpMsgIsEncrypt", SysConfigService.HTTP_MSG_IS_ENCRYPT));
		}else{
			result.put("sysConfig", JsonKit.newJson("httpMsgIsEncrypt", "1".equals(config.getConfigVal())?true:false));
		}
		log.info("机构端获取网站信息，请求地址：/api/anon/siteInfos，返回结果：{}", JSONObject.toJSONString(result));
		return ApiRes.ok(result);
	}

}
